<template>
	<view class="container">
		<uni-card :is-shadow="true" is-full style="background-color:  #F0EEEF;">
			<text class="uni-h6">题目上传器</text>
		</uni-card>
		
		<view class="uni1" style="background-color:  #F0EEEF;">
			<view class="title">是否批量上传</view>
			<radio-group name="radio" >
				<label>
					<radio value="radio1" color="#90A674" /><text>是</text>
				</label>
				<label>
					<radio value="radio2" color="#90A674"/><text>否</text>
				</label>
			</radio-group>
		</view>
		
		<uni-section title="上传选择" type="line" style="background-color:  #F0EEEF;">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="baseForm" :model="baseFormData" labelWidth="80px">
					
					<uni-forms-item label="上传时间">
						<uni-datetime-picker type="datetime" return-type="timestamp"
							v-model="baseFormData.datetimesingle" />
					</uni-forms-item>
					
					<uni-forms-item label="选择题库类型">
						<uni-data-select v-model="baseFormData.quesbankName"  
						 :localdata="quesbankName" popup-title="选择题库类型">
						</uni-data-select>
					</uni-forms-item>

				
				</uni-forms>
			</view>
		</uni-section>

		<uni-section title="题目具体" type="line" style="background-color:  #F0EEEF;">
			<view class="example">
				<!-- 基础题目具体 -->
				<uni-forms ref="valiForm" :rules="rules" :model="valiFormData" labelWidth="80px">
					<uni-forms-item label="题目类型" required name="questionType">
						<uni-easyinput v-model="valiFormData.questionType" placeholder="请输入单选/多选"/>
					</uni-forms-item>	
					<uni-forms-item label="题目" required name="content">
						<uni-easyinput v-model="valiFormData.content" placeholder="请输入题目"/>
					</uni-forms-item>
					<uni-forms-item label="答案" required name="answer">
						<uni-easyinput v-model="valiFormData.answer" placeholder="请输入答案" />
					</uni-forms-item>
				
				</uni-forms>
				<button type="primary" @click="submit('valiForm')" style="background-color:  #90A674;border-radius: 50rpx;width: 60%;margin-bottom: 10rpx;">提交</button>
			</view>
		</uni-section>

	


	</view>
</template>

<script>
import { data } from 'browserslist';
	export default {
		data() {
			return {
				
				// 基础表单数据
				baseFormData: {
					datetimesingle: Date(),
					quesbankName: "",
					
				},
				
				quesbankName:[
					{
						value:'',
						text:'',
					}
				],
		
				valiFormData:{
					questionType:"",
					content: "",
					answer: "",
					
				},
					
				
				
			
				// 校验规则
				rules: {
					questionType : {
						rules: [{
							required: true,
							errorMessage: '题目类型不能为空'
						}]
					},	
					content : {
						rules: [{
							required: true,
							errorMessage: '题目不能为空'
						}]
					},
					answer: {
						rules: [{
							required: true,
							errorMessage: '答案不能为空'
						}]
					}
				},
			
			}
		},
	
	mounted() {
	  // 发起后端请求获取数据库内容
	  
	  uni.request({
	    url: 'http://localhost:8888/getquesBankName',
	    method: 'GET',
	    success: (res) => {
	      // 请求成功的处理逻辑
	      console.log(res);
		  
		  var data = res.data.data;
		  this.quesbankName=data;
	       // 将数据库内容赋值给变量
		  console.log(data)
		  console.log(this.quesbankName)
	    },
	    fail: (err) => {
	      // 请求失败的处理逻辑
	      console.error(err);
	    },
	  });
	 
	 
	 
	},
		
	
		methods: {
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					console.log("ans:"+this.valiFormData.answer);
					var ans=this.valiFormData.answer;
					uni.request({
						url:"http://localhost:8888/addQuestion",
						method:"POST",
						data:{
							questionType:this.valiFormData.questionType,
							content:this.valiFormData.content,
							answer:this.valiFormData.answer,
						},
							success(res){
								if(res.data.code==0){
									console.log("成功",res);
									uni.setStorageSync('ans',ans);
									uni.setStorageSync('insertId',res.data.data.insertId);
									
									uni.navigateTo({
										url:"/pages/answer/answer"
								})
							}else{
								uni.showToast({
									title:"res.data.message",
									icon:"error"
								})
							}
						}
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
		}
	}
</script>

<style lang="scss">

	.uni-h6{
		font-size: 60rpx;
		color: #90A674;
		font-family: 隶书;
		padding-top: 40rpx;
		margin-left: 32%;
	}
	.uni1{
		display: flex;
		justify-content: space-between;
		padding-top: 20rpx;
	}


	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>
